{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\"\n",
    "using BaseTool;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `Enum` 定义 `Flags` 特性\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未定义 Flag 特性:\n",
      "  0 - None\n",
      "  1 - Black\n",
      "  2 - Red\n",
      "  3 - 3\n",
      "  4 - Green\n",
      "  5 - 5\n",
      "  6 - 6\n",
      "  7 - 7\n",
      "  8 - Blue\n",
      "  9 - 9\n",
      " 10 - 10\n",
      " 11 - 11\n",
      " 12 - 12\n",
      " 13 - 13\n",
      " 14 - 14\n",
      " 15 - 15\n",
      " 16 - 16\n"
     ]
    }
   ],
   "source": [
    "enum SingleHue : short\n",
    "{\n",
    "    None = 0,\n",
    "    Black = 1,\n",
    "    Red = 2,\n",
    "    Green = 4,\n",
    "    Blue = 8\n",
    "};\n",
    "\n",
    "Console.WriteLine(\"未定义 Flag 特性:\");\n",
    "for (int val = 0; val <= 16; val++)\n",
    "    Console.WriteLine(\"{0,3} - {1:G}\", val, (SingleHue)val);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "定义 Flag 特性:\n",
      "  0 - None\n",
      "  1 - Black\n",
      "  2 - Red\n",
      "  3 - Black, Red\n",
      "  4 - Green\n",
      "  5 - Black, Green\n",
      "  6 - Red, Green\n",
      "  7 - Black, Red, Green\n",
      "  8 - Blue\n",
      "  9 - Black, Blue\n",
      " 10 - Red, Blue\n",
      " 11 - Black, Red, Blue\n",
      " 12 - Green, Blue\n",
      " 13 - Black, Green, Blue\n",
      " 14 - Red, Green, Blue\n",
      " 15 - Black, Red, Green, Blue\n",
      " 16 - 16\n"
     ]
    }
   ],
   "source": [
    "[Flags]\n",
    "enum MultiHue : short\n",
    "{\n",
    "    None = 0,\n",
    "    Black = 1,\n",
    "    Red = 2,\n",
    "    Green = 4,\n",
    "    Blue = 8\n",
    "};\n",
    "\n",
    "Console.WriteLine();\n",
    "Console.WriteLine(\"定义 Flag 特性:\");\n",
    "for (int val = 0; val <= 16; val++)\n",
    "    Console.WriteLine(\"{0,3} - {1:G}\", val, (MultiHue)val);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意这里的16，没有定义的内容就显示原值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Enum`类型默认继承`int`，可以显示定义继承其他整型（上面的 `short`），但是这个知识没什么用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Green\n",
      "4\n",
      "4\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "SingleHue.Green.Dump();\n",
    "((int)SingleHue.Green).Dump();\n",
    "SingleHue.Green.GetHashCode().Dump();\n",
    "Convert.ToUInt32(SingleHue.Green).Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在使用Enum.ToObject时，如果传入的值超过了枚举类型的范围，也不会抛出异常，而是返回对应的int值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submission#2+SingleHue\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "SingleHue _obj = (SingleHue)Enum.ToObject(typeof(SingleHue), 16);\n",
    "_obj.GetType().Dump();\n",
    "_obj.Dump();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "languageName": "csharp",
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
